#!/bin/python
'''
NAME
    gbaoip - A program used to connect two GameBoy Advance devices over the 
             internet. 

SYNOPSIS
    gbaoip [-fhmv] [remote-address]:[port]
    
DESCRIPTION
    gbaoip is an program provided to enable a user to play Multiplayer on 
    archaic GameBoy Advance devices over the internet. It is intended to be
    only for this purpose.
    
    The options are as follows:
    
    -f, --first-player  set this running instance of gbaoip to act as the first
                        player/server. The remove-address is ignored, and port
                        is the port to open a socket on for remote users to 
                        connect.
    
    -h, --help          bring up this help page
    
    -m, --multi=NUMBER
                        number of players(2-4), defaults to 2
    
    -v, --version       display the version number
    
    
    The remote address is to be specified in IPv4, dotted decimal format - i.e.
    192.168.1.1. The remote address must be reachable, and the port number must
    be valid. (in both server and client modes)
    
    
NOTES
    This program is expected to be run on a Raspberry Pi, with GPIO pins 
    connected to a modified GBA inter-device link. 
    
BUGS
    Stay tuned.
    
LICENSE
    The MIT License (MIT)

    Copyright (c) 2014 Thomas Quinn and Paul Laurenzano

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to 
    deal in the Software without restriction, including without limitation the 
    rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
    sell copies of the Software, and to permit persons to whom the Software is 
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.
'''

import sys, getopt
from process import process
from firstplayer import firstplayer_setup

version = "0.1"

class Usage(Exception):
    def __init__(self, msg):
        self.msg = msg

def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "fhmv", ["first-player", 
                                                "help", "multi=", "version"])
            if len(args)!=1:
                print "Invalid number of arguments provided. See the help (-h) \
                for more information"
                return 0
        except getopt.error, msg:
            raise Usage(msg)
        # process options
        for option, argument in opts:
            if option in ("-h", "--help"):
                print __doc__
                sys.exit(0)
            elif option in ("-v", "--version"):
                print "Version number: "+version
                sys.exit(0)
            if option in ("-f", "--first-player"):
                #TODO - Implement Server
                firstplayer_setup(args)
            if option in ("-m", "--multi"):
                #TODO - Implement Multiplayer
                print "MULTIPLAYER"
        
        # process arguments
        #for arg in args:
        #    process(arg) # process() is defined elsewhere

    except Usage, err:
        print >>sys.stderr, err.msg
        print >>sys.stderr, "for help use --help or -h"
        return 2
##################################    MAIN    ##################################
if __name__ == "__main__":
    #print "Main:"
    sys.exit(main())
    